import store from '../src/store/index.js';
var flag = true; //判断路况图是否在底图中
var drawLayer;
var drawLayer1;
var drawLayer2;
function querySuccess(result) {
  if (flag) {
    var map = store.state.map;
    //   初始化Zondy.Format.PolygonJSON类
    var format = new Zondy.Format.PolygonJSON();
    //将MapGIS要素JSON反序列化为ol.Feature类型数组
    var features = format.read(result);

    var red = [];
    var yellow = [];
    var green = [];
    // 判断
    //  >1500 红色 1000-1500 黄色 <1000 绿色
    features.map((item) => {
      if (item.values_) {
        var m = item.values_.values_.车流量;
        if (m >= 1500) {
          red.push(item);
        } else if (m < 1500 && m >= 1000) {
          yellow.push(item);
        } else if (m < 1000) {
          green.push(item);
        }
      }
    });

    var red_new = [];
    var app = red.forEach((item) => {
      if (item !== undefined) {
        red_new.push(item);
      }
    });

    //实例化一个矢量图层drawLayerr用于高亮显示结果
    var drawSource = new ol.source.Vector({
      wrapX: false,
      features: red_new,
    });
    // drawSource.addFeature(features);
    drawLayer = new ol.layer.Vector({
      source: drawSource,
      style: new ol.style.Style({
        //填充色
        fill: new ol.style.Fill({
          color: '#ac0a12',
        }),
        //边线样式
        stroke: new ol.style.Stroke({
          color: '#ac0a12',
          width: 5,
        }),
      }),
    });

    var yellow_new = [];
    var app = yellow.forEach((item) => {
      if (item !== undefined) {
        yellow_new.push(item);
      }
    });

    //实例化一个矢量图层drawLayerr用于高亮显示结果
    var drawSource1 = new ol.source.Vector({
      wrapX: false,
      features: yellow_new,
    });
    // drawSource.addFeature(features);
    drawLayer1 = new ol.layer.Vector({
      source: drawSource1,
      style: new ol.style.Style({
        //填充色
        fill: new ol.style.Fill({
          color: 'yellow',
        }),
        //边线样式
        stroke: new ol.style.Stroke({
          color: 'yellow',
          width: 5,
        }),
      }),
    });

    var green_new = [];
    var app = green.forEach((item) => {
      if (item !== undefined) {
        green_new.push(item);
      }
    });

    //实例化一个矢量图层drawLayerr用于高亮显示结果
    var drawSource2 = new ol.source.Vector({
      wrapX: false,
      features: green_new,
    });
    // drawSource.addFeature(features);
    drawLayer2 = new ol.layer.Vector({
      source: drawSource2,
      style: new ol.style.Style({
        //填充色
        fill: new ol.style.Fill({
          color: '#55d37f',
        }),
        //边线样式
        stroke: new ol.style.Stroke({
          color: '#55d37f',
          width: 5,
        }),
      }),
    });
    map.addLayer(drawLayer);
    map.addLayer(drawLayer1);
    map.addLayer(drawLayer2);
    map.setView(
      new ol.View({
        center: [114.37, 30.5],
        zoom: 13,
        projection: 'EPSG:4326',
      })
    );
    flag = false;
  } else {
    drawLayer.setVisible(false);
    drawLayer1.setVisible(false);
    drawLayer2.setVisible(false);
    flag = true;
  }
}
export { querySuccess };
